package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.UserMapper;
import com.tedu.weibo.pojo.dto.UserLoginDTO;
import com.tedu.weibo.pojo.dto.UserRegDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.UserVO;
import com.tedu.weibo.result.JsonResult;
import com.tedu.weibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/users")
@Slf4j
@Api(tags = "用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/reg")
    @ApiOperation("用户注册")
    /*
    * 如果浏览器在小西正文中以JSON格式传递数据时,要在参数DTO上家注解@RequestBody
    * */
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO){
        //trace可以在程序执行的关键节点进行记录,让程序员了解程序的执行过程
        log.trace("UserController.reg()开始调用...");
        //info可以记录程序需要的一些正常提示信息
        log.info("开始处理用户注册");
        //1.验证是否为重复用户
        if (userMapper.selectByIdUsername(userRegDTO.getUsername())!=null){
            //重复用户
            return new JsonResult(Status.USER_EXIST);
        }
        System.out.println(userRegDTO);
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        userMapper.insertUser(user);
        log.trace("UserController.reg()调用完毕");
        return JsonResult.success();
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        System.out.println("开始处理用户登录,"+userLoginDTO);
        User user = userMapper.selectByIdUsername(userLoginDTO.getUsername());
        if (user == null) {
            //用户名错误
            return new JsonResult(Status.USERNAME_ERROR);
        }else if (!userLoginDTO.getPassword().equals(user.getPassword())){
            //密码错误
            return new JsonResult(Status.PASSWORD_ERROR);
        }else {
            //登录成功
            //将当前用户的信息存入session
            session.setAttribute("user",user);
            //System.out.println(user);
            return JsonResult.success();
        }
    }

    @GetMapping("/currentUser")
    @ApiOperation("查看用户信息")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user!=null){
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user,userVO);
            return JsonResult.success(userVO);
        }
        return new JsonResult(Status.NOT_LOGIN);
    }

    @GetMapping("/logout")
    @ApiOperation("用户登出")
    public  JsonResult logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
        return JsonResult.success();
    }
}
